home *** CD-ROM | disk | FTP | other *** search
- /*-------------------------------------------------------------------
-
- AOCE Post Office Protocol (POP) / Simple Mail Transfer Protocol (SMTP)
- Mail Service Access Module
-
- written by Steve Falkenburg-- MacDTS
- ©1991-1993 Apple Computer, Inc.
-
- --------------
- change history
- --------------
-
- SJF 02/19/93 update for beta build b1
- SJF 10/29/92 update to a11 a11
- SJF 06/08/92 update to a8 a8
- SJF 02/15/92 first working version a4.5
- SJF 10/16/91 initial coding a3
-
- ---------------------------------------------------------------------*/
-
- #ifndef __TYPES__
- #include <Types.h>
- #endif
-
- #ifndef __OCE__
- #include <OCE.h>
- #endif
-
- #ifndef __OCEMAIL__
- #include <OCEMail.h>
- #endif
-
- #include <string.h>
-
- #include "const.h"
- #include "gwerrors.h"
- #include "mytypes.h"
- #include "globals.h"
- #include "utils.h"
- #include "spoolsystem.h"
- #include "gatewaystuff.h"
- #include "smtp.protocol.h"
- #include "convertaddress.h"
-
- #include "spooltoexternal.h"
-
- #define kMaxMsgSize 64000
- #define kBCCBlockSize 2048
-
- #define kFromHeader "From: "
- #define kToHeader "To: "
- #define kCCHeader "Cc: "
- #define kBCCHeader "Bcc: "
- #define kSubjectHeader "Subject: "
- #define kAddressDelimiter ", "
-
- OSErr SpoolToExternalGW(FSSpec *spoolSpec,SlotSpec *slotSpec)
- {
- unsigned long smtpServerAddress;
- OSErr err;
- char tmpString[256];
- char bccBlock[256];
- char fromAddr[256];
- char *textBlock;
- char *messageBlock;
- unsigned long textBlockLen,textBlockStart,tmpLen;
- RString tmpRString;
- char packedRecip[kMaxRecipSize];
- short recipIndex;
- Boolean hasRecipient;
-
- // allocate memory
-
- messageBlock = (char *)NewPtrChk(kMaxMsgSize);
- if (MemError()!=noErr) {
- return BailOnSend(MemError(),nil);
- }
- messageBlock[0] = 0;
-
- // get IP address of SMTP server
-
- err = ConvertStringToAddr(slotSpec->specInfo.smtpServer,&smtpServerAddress);
- if (err!=noErr) {
- return BailOnSend(kInvalidSMTPServer,messageBlock);
- }
-
- // build from address (from POP account)
-
- strcpy(fromAddr,slotSpec->dirIdentity.userName);
- strcat(fromAddr,"@");
- strcat(fromAddr,slotSpec->specInfo.popServer);
- strcpy(messageBlock,kFromHeader);
- strcat(messageBlock,fromAddr);
- strcat(messageBlock,kCRStr);
-
- // build to address
-
- hasRecipient = false;
- strcat(messageBlock,kToHeader);
- for (err=noErr,recipIndex=0; err==noErr; recipIndex++) {
- tmpLen = kMaxRecipSize;
- err = GetFromSpool(spoolSpec,kToType,kAddrCreator,recipIndex,(Ptr)packedRecip,&tmpLen,0);
- if (err==noErr) {
- if (TranslateAddress((OCEPackedRecipient *)packedRecip,tmpString)) {
- strcat(messageBlock,tmpString);
- strcat(messageBlock,kAddressDelimiter);
- hasRecipient = true;
- }
- }
- }
- if (hasRecipient) {
- messageBlock[strlen(messageBlock)-strlen(kAddressDelimiter)] = 0;
- strcat(messageBlock,kCRStr);
- }
- else {
- messageBlock[strlen(messageBlock)-strlen(kToHeader)] = 0;
- }
-
- // build cc address
-
- hasRecipient = false;
- strcat(messageBlock,kCCHeader);
- for (err=noErr,recipIndex=0; err==noErr; recipIndex++) {
- tmpLen = kMaxRecipSize;
- err = GetFromSpool(spoolSpec,kCCType,kAddrCreator,recipIndex,(Ptr)packedRecip,&tmpLen,0);
- if (err==noErr) {
- if (TranslateAddress((OCEPackedRecipient *)packedRecip,tmpString)) {
- strcat(messageBlock,tmpString);
- strcat(messageBlock,kAddressDelimiter);
- hasRecipient = true;
- }
- }
- }
- if (hasRecipient) {
- messageBlock[strlen(messageBlock)-strlen(kAddressDelimiter)] = 0;
- strcat(messageBlock,kCRStr);
- }
- else {
- messageBlock[strlen(messageBlock)-strlen(kCCHeader)] = 0;
- }
-
- // build bcc address (and store in separate string)
-
- hasRecipient = false;
- strcpy(bccBlock,kBCCHeader);
- for (err=noErr,recipIndex=0; err==noErr; recipIndex++) {
- tmpLen = kMaxRecipSize;
- err = GetFromSpool(spoolSpec,kBCCType,kAddrCreator,recipIndex,(Ptr)packedRecip,&tmpLen,0);
- if (err==noErr) {
- if (TranslateAddress((OCEPackedRecipient *)packedRecip,tmpString)) {
- strcat(bccBlock,tmpString);
- strcat(bccBlock,kAddressDelimiter);
- hasRecipient = true;
- }
- }
- }
- if (hasRecipient) {
- messageBlock[strlen(messageBlock)-strlen(kAddressDelimiter)] = 0;
- strcat(bccBlock,kCRStr);
- }
-
- // build subject
-
- strcat(messageBlock,kSubjectHeader);
- tmpLen = kRStringMaxBytes;
- err = GetFromSpool(spoolSpec,kSubjectType,kAttribCreator,0,(Ptr)&tmpRString,&tmpLen,0);
- if (err==noErr) {
- r2cString(&tmpRString,tmpString);
- }
- else
- strcpy(tmpString,"<none>");
- strcat(messageBlock,tmpString);
- strcat(messageBlock,kCRStr);
- strcat(messageBlock,kCRStr);
-
- // end message header
-
- textBlockStart = strlen(messageBlock);
- textBlock = messageBlock+textBlockStart;
-
- // build body
-
- textBlockLen = kMaxMsgSize-textBlockStart;
- err = GetFromSpool(spoolSpec,kTextContent,kContentCreator,0,textBlock,&textBlockLen,0);
- if (err!=noErr)
- return BailOnSend(err,messageBlock);
- textBlock[textBlockLen] = 0;
-
- err = SendSMTP(messageBlock,bccBlock,fromAddr,smtpServerAddress);
- if (err!=noErr) {
- return BailOnSend(kInvalidSMTPServer,messageBlock);
- }
-
- DisposPtrChk(messageBlock);
- return noErr;
- }
-
-
- OSErr BailOnSend(OSErr err,Ptr messageBlock)
- {
- if (messageBlock)
- DisposPtrChk(messageBlock);
-
- return err;
- }
-
-
- Boolean TranslateAddress(OCEPackedRecipient *pRecip,char *unixRecip)
- {
- OCERecipient rcpt;
- RecordID entitySpecifier;
- OSType recipType;
- RString recipRStr;
-
- OCEUnpackDSSpec((PackedDSSpec*)pRecip,&rcpt,&entitySpecifier);
- recipType = rcpt.extensionType;
- switch (recipType) {
- case kPopAddrType:
- BlockMove(rcpt.extensionValue,&recipRStr,rcpt.extensionSize); // should probably do some range check
- r2cString(&recipRStr,unixRecip);
- break;
- default: // punt on string representation of aoce addresses for now
- return false;
- break;
- }
- return true;
- }
-